<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="google-site-verification" content="VGgQeH6NiuAKspyCFT7dqUNmNhg6RJoMYQXErdy0jgE" />
  <meta name="baidu-site-verification" content="code-yNmdsKJ9GP" />
  
  
  <meta name="keywords" content="Elasticsearch,elasticsearch,搭建，环境搭建,">
  
  
  <meta name="description" content="Elasticsearch 环境搭建">
  
  <title>
    2.Elasticsearch 快速入门 |
    
    思远程序
  </title>
  <!-- Icon -->
  
    <link rel="shortcut icon" href="/favicon.ico">
    
  
<link rel="stylesheet" href="/css/style.css">

  
  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<script src="/js/pace.min.js"></script>

<meta name="generator" content="Hexo 6.2.0"></head>

<body>
  <main class="content">
    <section class="outer">
  <article id="post-2. Elasticsearch 快速入门" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
      

<h1 class="article-title" itemprop="name">
  2.Elasticsearch 快速入门
</h1>



    </header>
    

    
    <div class="article-meta">
      <a href="/2022/07/23/2.%20Elasticsearch%20%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/" class="article-date">
  <time datetime="2022-07-23T02:00:00.000Z" itemprop="datePublished">2022-07-23</time>
</a>
      
<div class="article-category">
  <a class="article-category-link" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E6%A1%86%E6%9E%B6/">分布式框架</a>
</div>

    </div>
    

    
    
<div class="tocbot"></div>

    

    <div class="article-entry" itemprop="articleBody">
      
      
      
      <p>设想一个关于搜索的场景，假设我们要搜索一首诗句内容中带“前”字的古诗<br><img src="https://cdn.nlark.com/yuque/0/2022/png/1445568/1656916928026-dd0f1faa-4ff3-4ef3-819d-2897eb7a6b4a.png" alt="image.png"><br>思考：用传统关系型数据库和ES 实现会有什么差别？</p>
<span id="more"></span>

<p>如果用像 MySQL 这样的 RDBMS 来存储古诗的话，我们应该会去使用这样的 SQL 去查询 1<br><code>select name from poems where content like &quot;%前%&quot; </code><br>这种我们称为顺序扫描法，需要遍历所有的记录进行匹配。不但效率低，而且不符合我们搜索时的期望，比如我们在搜索“ABCD”这样的关键词时，通常还希望看 到”A”,”AB”,”CD”,“ABC”的搜索结果。</p>
<h2 id="什么是全文检索"><a href="#什么是全文检索" class="headerlink" title="什么是全文检索"></a>什么是全文检索</h2><p>全文检索是指：</p>
<ul>
<li>通过一个程序扫描文本中的每一个单词，针对单词建立索引，并保存该单词在文 本中的位置、以及出现的次数;</li>
<li>用户查询时，通过之前建立好的索引来查询，将索引中单词对应的文本位置、出 现的次数返回给用户，因为有了具体文本的位置，所以就可以将具体内容读取出来了</li>
</ul>
<p><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280040431.png" alt="image.png"><br>Elasticsearch 搜索原理简单概括的话可以分为这么几步： </p>
<ol>
<li>内容爬取，停顿词过滤比如一些无用的像”的”，“了”之类的语气词&#x2F;连接词;</li>
<li>内容分词，提取关键词；</li>
<li>根据关键词建立倒排索引；</li>
<li>用户输入关键词进行搜索(提取用户输入的关键词，进行分词再搜索)；</li>
</ol>
<h3 id="倒排索引"><a href="#倒排索引" class="headerlink" title="倒排索引"></a>倒排索引</h3><p>索引就类似于目录，平时我们使用的都是索引，都是通过主键定位到某条数据， 那么倒排索引呢，刚好相反，数据对应到主键。<img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280041369.png" alt="image.png"></p>
<h4 id="正排索引-正向索引"><a href="#正排索引-正向索引" class="headerlink" title="正排索引(正向索引)"></a>正排索引(正向索引)</h4><p><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280041644.png" alt="image.png"></p>
<h4 id="倒排索引-反向索引"><a href="#倒排索引-反向索引" class="headerlink" title="倒排索引(反向索引)"></a>倒排索引(反向索引)</h4><p>假如，我们有一个站内搜索的功能，通过某个关键词来搜索相关的文章，那么这个关键词可 能出现在标题中，也可能出现在文章内容中，那我们将会在创建或修改文章的时候，建立一 个关键词与文章的对应关系表，这种，我们可以称之为倒排索引。 like %java设计模式% java 设计模式。<br><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280041897.png" alt="image.png"><br>简单理解，正向索引是通过 key 找 value，反向索引则是通过value找key。ES底层在检索时底层使用的就是倒排索引。</p>
<h3 id="ElasticSearch-vs-Solr"><a href="#ElasticSearch-vs-Solr" class="headerlink" title="ElasticSearch vs Solr"></a>ElasticSearch vs Solr</h3><p>Solr 是第一个基于 Lucene 核心库功能完备的搜索引擎产品，诞生远早于 Elasticsearch。 当单纯的对已有数据进行搜索时，Solr更快。当实时建立索引时, Solr会产生io阻 塞，查询性能较差, Elasticsearch具有明显的优势。<br><strong>总结</strong>： </p>
<ul>
<li>Solr 利用 Zookeeper 进行分布式管理，而Elasticsearch 自身带有分布式协调 管理功能。 </li>
<li>Solr 支持更多格式的数据，比如JSON、XML、CSV，而 Elasticsearch 仅支持 json文件格式。</li>
<li>Solr 在传统的搜索应用中表现好于 Elasticsearch，但在处理实时搜索应用时效 率明显低于 Elasticsearch。 </li>
<li>Solr 是传统搜索应用的有力解决方案，但 Elasticsearch更适用于新兴的实时搜 索应用。</li>
</ul>
<h2 id="ElasticSearch-基本概念"><a href="#ElasticSearch-基本概念" class="headerlink" title="ElasticSearch 基本概念"></a>ElasticSearch 基本概念</h2><h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><p>Elasticsearch是面向文档(document oriented)的，这意味着它可以存储整个对象或文档(document)。然而它不仅<br>仅是存储，还会索引(index)每个文档的内容使之可以被搜索。在Elasticsearch中，你可以对文档（而非成行成列的数据）进行索引、搜索、排序、过滤。Elasticsearch比传统关系型数据库如下：<br><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280042027.png" alt="image.png"></p>
<h3 id="核心概念"><a href="#核心概念" class="headerlink" title="核心概念"></a>核心概念</h3><h4 id="index索引"><a href="#index索引" class="headerlink" title="index索引"></a>index索引</h4><ul>
<li>一个索引就是一个拥有几分相似特征的文档的集合。比如说，你可以有一个客户数据的索引，另一个产品目录的索引，还有一个订单数据的索引；</li>
<li>一个索引由一个名字来标识（必须全部是小写字母的），并且当我们要对对应于这个索引中的文档进行索引、搜索、更新和删除的时候，都要使用到这个名字。<strong>可类比mysql中的数据库</strong></li>
</ul>
<h4 id="type类型"><a href="#type类型" class="headerlink" title="type类型"></a>type类型</h4><p>在一个索引中，你可以定义一种或多种类型。一个类型是你的索引的一个逻辑上的分类&#x2F;分区，其语义完全由你来定。通常，会为具有一组共同字段的文档定义一个类型。比如说，我们假设你运营一个博客平台并且将你所有的数据存储到一个索引中。在这个索引中，你可以为用户数据定义一个类型，为博客数据定义另一个类型，当然，也可以为评论数据定义另一个类型。 <strong>可类比mysql中的表</strong></p>
<h4 id="document文档"><a href="#document文档" class="headerlink" title="document文档"></a>document文档</h4><ul>
<li>一个文档是一个可被索引的基础信息单元。比如，你可以拥有某一个客户的文档，某一个产品的一个文档，当然，也可以拥有某个订单的一个文档</li>
<li>文档以JSON（Javascript Object Notation）格式来表示，而JSON是一个到处存在的互联网数据交互格式。在一个index&#x2F;type里面，你可以存储任意多的文档。注意，尽管一个文档，物理上存在于一个索引之中，文档必须被索引&#x2F;赋予一个索引的type。 <strong>插入索引库以文档为单位，类比与数据库中的一行数据</strong>；</li>
<li>每个文档都有一个Unique ID，可以自己指定ID或者通过Elasticsearch自动生成</li>
<li>一篇文档包含了一系列字段，类似数据库表中的一条记录；</li>
<li>JSON文档，格式灵活，不需要预先定义格式，字段的类型可以指定或者通过Elasticsearch自动推算，支持数组&#x2F;支持嵌套</li>
</ul>
<h4 id="Filed字段"><a href="#Filed字段" class="headerlink" title="Filed字段"></a>Filed字段</h4><p>相当于是数据表的字段，对文档数据根据不同属性进行的分类标识 。</p>
<h4 id="文档元数据"><a href="#文档元数据" class="headerlink" title="文档元数据"></a>文档元数据</h4><p><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280042589.png" alt="image.png"><br>元数据，用于标注文档的相关信息： </p>
<ul>
<li>_index：文档所属的索引名 </li>
<li>_type：文档所属的类型名 </li>
<li>_id：文档唯—ld </li>
<li>_source: 文档的原始Json数据 </li>
<li>_version: 文档的版本号，修改删除操作_version都会自增1 </li>
<li>_seq_no: 和_version一样，一旦数据发生更改，数据也一直是累计的。 Shard级别严格递增，保证后写入的Doc的_seq_no大于先写入的Doc的_seq_no。 </li>
<li>_primary_term: _primary_term主要是用来恢复数据时处理当多个文档的 _seq_no一样时的冲突，避免Primary Shard上的写入被覆盖。每当Primary Shard 发生重新分配时，比如重启，Primary选举等，_primary_term会递增1。</li>
</ul>
<h4 id="映射mapping"><a href="#映射mapping" class="headerlink" title="映射mapping"></a>映射mapping</h4><p>mapping是处理数据的方式和规则方面做一些限制，如某个字段的数据类型、默认值、分析器、是否被索引等等，这些都是映射里面可以设置的，其它就是处理es里面数据的一些使用规则设置也叫做映射，按着最优规则处理数据对性能提高很大，因此才需要建立映射，并且需要思考如何建立映射才能对性能更好。<strong>相当于mysql中的创建表的过程，设置主键外键等等</strong></p>
<h4 id="集群cluster"><a href="#集群cluster" class="headerlink" title="集群cluster"></a>集群cluster</h4><p>一个集群就是由一个或多个节点组织在一起，它们共同持有整个的数据，并一起提供索引和搜索功能。一个集群由 一个唯一的名字标识，这个名字默认就是“elasticsearch”。这个名字是重要的，因为一个节点只能通过指定某个集 群的名字，来加入这个集群。</p>
<h4 id="节点node"><a href="#节点node" class="headerlink" title="节点node"></a>节点node</h4><p>一个节点是集群中的一个服务器，作为集群的一部分，它存储数据，参与集群的索引和搜索功能。和集群类似，一 个节点也是由一个名字来标识的，默认情况下，这个名字是一个随机的漫威漫画角色的名字，这个名字会在启动的 时候赋予节点。这个名字对于管理工作来说挺重要的，因为在这个管理过程中，你会去确定网络中的哪些服务器对 应于Elasticsearch集群中的哪些节点。<br>一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下，每个节点都会被安排加入到一个叫 做“elasticsearch”的集群中，这意味着，如果你在你的网络中启动了若干个节点，并假定它们能够相互发现彼此， 它们将会自动地形成并加入到一个叫做“elasticsearch”的集群中。<br>在一个集群里，只要你想，可以拥有任意多个节点。而且，如果当前你的网络中没有运行任何Elasticsearch节点， 这时启动一个节点，会默认创建并加入一个叫做“elasticsearch”的集群。</p>
<h4 id="分片和复制-shards-amp-replicas"><a href="#分片和复制-shards-amp-replicas" class="headerlink" title="分片和复制 shards&amp;replicas"></a>分片和复制 shards&amp;replicas</h4><p>一个索引可以存储超出单个结点硬件限制的大量数据。比如，一个具有10亿文档的索引占据1TB的磁盘空间，而任一节点都没有这样大的磁盘空间；或者单个节点处理搜索请求，响应太慢。为了解决这个问题，Elasticsearch提供了将索引划分成多份的能力，这些份就叫做分片。当你创建一个索引的时候，你可以指定你想要的分片的数量。每个分片本身也是一个功能完善并且独立的“索引”，这个“索引”可以被放置到集群中的任何节点上。分片很重要，主要有两方面的原因： 1）允许你水平分割&#x2F;扩展你的内容容量。 2）允许你在分片（潜在地，位于多个节点上）之上进行分布式的、并行的操作，进而提高性能&#x2F;吞吐量。<br>至于一个分片怎样分布，它的文档怎样聚合回搜索请求，是完全由Elasticsearch管理的，对于作为用户的你来说，这些都是透明的。<br>在一个网络&#x2F;云的环境里，失败随时都可能发生，在某个分片&#x2F;节点不知怎么的就处于离线状态，或者由于任何原因消失了，这种情况下，有一个故障转移机制是非常有用并且是强烈推荐的。为此目的，Elasticsearch允许你创建分片的一份或多份拷贝，这些拷贝叫做复制分片，或者直接叫复制。<br>复制之所以重要，有两个主要原因： 在分片&#x2F;节点失败的情况下，提供了高可用性。因为这个原因，注意到复制分片从不与原&#x2F;主要（original&#x2F;primary）分片置于同一节点上是非常重要的。扩展你的搜索量&#x2F;吞吐量，因为搜索可以在所有的复制上并行运行。总之，每个索引可以被分成多个分片。一个索引也可以被复制0次（意思是没有复制）或多次。一旦复制了，每个索引就有了主分片（作为复制源的原来的分片）和复制分片（主分片的拷贝）之别。分片和复制的数量可以在索引创建的时候指定。在索引创建之后，你可以在任何时候动态地改变复制的数量，但是你事后不能改变分片的数量。<br>默认情况下，Elasticsearch中的每个索引被分片5个主分片和1个复制，这意味着，如果你的集群中至少有两个节点，你的索引将会有5个主分片和另外5个复制分片（1个完全拷贝），这样的话每个索引总共就有10个分片。</p>
<h3 id="IK-分词器插件"><a href="#IK-分词器插件" class="headerlink" title="IK 分词器插件"></a>IK 分词器插件</h3><p>官网现在地址：<a target="_blank" rel="noopener" href="https://github.com/medcl/elasticsearch-analysis-ik">https://github.com/medcl/elasticsearch-analysis-ik</a><br>根据 ElasticSearch 版本下载对应的 IK 分词器的版本，否则启动 ElasticSearch 会报错。<br>下载完成之后，解压，存放在 ElasticSearch–&gt;plugins目录下。<br>启动 ElasticSearch<br><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280039696.png"></p>
<p>IK 分词器提供了两种算法，ik_smart 和 ik_max_word，其中ik_smart为最少切分，ik_max_word为最细粒度划分。<br><strong>ik_smart：</strong><br><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280039671.png"><br><strong>ik_max_word：</strong><br><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280039456.png"></p>
<h4 id="分词的目的"><a href="#分词的目的" class="headerlink" title="分词的目的"></a>分词的目的</h4><p>内容分词，提取关键字根据关键字建立倒排索引。<br>在搜搜时，对所有的内容进行分词，然后在倒排索引中查找，最终定位到具体信息。</p>
<h4 id="自定义分词"><a href="#自定义分词" class="headerlink" title="自定义分词"></a>自定义分词</h4><p><strong>IKAnalyzer.cfg.xml</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">properties</span> <span class="keyword">SYSTEM</span> <span class="string">&quot;http://java.sun.com/dtd/properties.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">comment</span>&gt;</span>IK Analyzer 扩展配置<span class="tag">&lt;/<span class="name">comment</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!--用户可以在这里配置自己的扩展字典 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">&quot;ext_dict&quot;</span>&gt;</span>siyuan.dic<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span><br><span class="line">	 <span class="comment">&lt;!--用户可以在这里配置自己的扩展停止词字典--&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">&quot;ext_stopwords&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!--用户可以在这里配置远程扩展字典 --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- &lt;entry key=&quot;remote_ext_dict&quot;&gt;words_location&lt;/entry&gt; --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!--用户可以在这里配置远程扩展停止词字典--&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- &lt;entry key=&quot;remote_ext_stopwords&quot;&gt;words_location&lt;/entry&gt; --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>siyuan.dic</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">打工人</span><br></pre></td></tr></table></figure>
<p><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280039111.png"></p>
<h3 id="Rest-风格"><a href="#Rest-风格" class="headerlink" title="Rest 风格"></a>Rest 风格</h3><p><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280039650.png"></p>
<h4 id="创建文档"><a href="#创建文档" class="headerlink" title="创建文档"></a>创建文档</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">PUT /siyuan/es/<span class="number">1</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;siyuan&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span><span class="string">&quot;18&quot;</span> </span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"></span><br><span class="line">response<span class="punctuation">:</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;_index&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;siyuan&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_type&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;es&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;1&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_version&quot;</span> <span class="punctuation">:</span> <span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;result&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;created&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_shards&quot;</span> <span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;total&quot;</span> <span class="punctuation">:</span> <span class="number">2</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;successful&quot;</span> <span class="punctuation">:</span> <span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;failed&quot;</span> <span class="punctuation">:</span> <span class="number">0</span></span><br><span class="line">  <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_seq_no&quot;</span> <span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_primary_term&quot;</span> <span class="punctuation">:</span> <span class="number">6</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<p><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280039445.png"></p>
<h4 id="指定字段类型"><a href="#指定字段类型" class="headerlink" title="指定字段类型"></a>指定字段类型</h4><p><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280039689.png"></p>
<p><img src="https://siyit-blog.oss-cn-hangzhou.aliyuncs.com/images/2022202207280039317.png"></p>
<h4 id="修改文档"><a href="#修改文档" class="headerlink" title="修改文档"></a>修改文档</h4><p><strong>暴力修改</strong>：直接覆盖</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">POST /siyuan/es/<span class="number">1</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;siyuan123&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span><span class="string">&quot;18&quot;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line">----------------------------response-----------------------------</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;_index&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;siyuan&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_type&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;es&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;1&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_version&quot;</span> <span class="punctuation">:</span> <span class="number">2</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_seq_no&quot;</span> <span class="punctuation">:</span> <span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_primary_term&quot;</span> <span class="punctuation">:</span> <span class="number">6</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;found&quot;</span> <span class="punctuation">:</span> <span class="keyword">true</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_source&quot;</span> <span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;name&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;siyuan123&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;age&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;18&quot;</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="修改语句修改"><a href="#修改语句修改" class="headerlink" title="修改语句修改"></a>修改语句修改</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">POST /siyuan/es/<span class="number">2</span>/_update</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;doc&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;siyuan234&quot;</span> </span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line">------------------------response------------------------</span><br><span class="line">GET /siyuan/es/<span class="number">2</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;_index&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;siyuan&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_type&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;es&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;2&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_version&quot;</span> <span class="punctuation">:</span> <span class="number">2</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_seq_no&quot;</span> <span class="punctuation">:</span> <span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_primary_term&quot;</span> <span class="punctuation">:</span> <span class="number">6</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;found&quot;</span> <span class="punctuation">:</span> <span class="keyword">true</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;_source&quot;</span> <span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;name&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;siyuan234&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;age&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;18&quot;</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="查询文档"><a href="#查询文档" class="headerlink" title="查询文档"></a>查询文档</h4><p><strong>根据id查询</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /siyuan/es/2</span><br></pre></td></tr></table></figure>

<p><strong>根据关键字查询</strong></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 查询 name = siyuan2 的数据</span></span><br><span class="line">POST /siyuan/es/_search</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;query&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;match&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">      <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span> <span class="string">&quot;siyuan2&quot;</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 查询</span></span><br></pre></td></tr></table></figure>

<h2 id="遇到问题"><a href="#遇到问题" class="headerlink" title="遇到问题"></a>遇到问题</h2><h3 id="错误1：future-versions-of-Elasticsearch-will-require-Java-11-your-Java-version-from"><a href="#错误1：future-versions-of-Elasticsearch-will-require-Java-11-your-Java-version-from" class="headerlink" title="错误1：future versions of Elasticsearch will require Java 11; your Java version from"></a>错误1：future versions of Elasticsearch will require Java 11; your Java version from</h3><p><strong>原因：</strong>说明这个版本对应的jdk应该是java11。我们环境中的jdk是java8。其实新版的es是自带了jdk的。但是和我们环境变量的冲突了。</p>
<p><strong>解决方案：</strong>修改启动文件,添加如下修改</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">38 # now set the path to java</span><br><span class="line">39 if [ ! -z &quot;$JAVA_HOME&quot; ]; then</span><br><span class="line">40   JAVA=&quot;$JAVA_HOME/bin/java&quot;</span><br><span class="line">41   JAVA_TYPE=&quot;JAVA_HOME&quot;</span><br><span class="line">42 else</span><br><span class="line">43   if [ &quot;$(uname -s)&quot; = &quot;Darwin&quot; ]; then</span><br><span class="line">44     # macOS has a different structure</span><br><span class="line">45     JAVA=&quot;$ES_HOME/jdk.app/Contents/Home/bin/java&quot;</span><br><span class="line">46   else</span><br><span class="line">47     JAVA=&quot;$ES_HOME/jdk/bin/java&quot;</span><br><span class="line">48   fi</span><br><span class="line">49   JAVA_TYPE=&quot;bundled jdk&quot;</span><br><span class="line">50 fi</span><br></pre></td></tr></table></figure>

<h3 id="错误3：ElasticsearchException-X-Pack-is-not-supported-and-Machine-Learning-is-not-available-for-windows"><a href="#错误3：ElasticsearchException-X-Pack-is-not-supported-and-Machine-Learning-is-not-available-for-windows" class="headerlink" title="错误3：ElasticsearchException[X-Pack is not supported and Machine Learning is not available for [windows"></a>错误3：ElasticsearchException[X-Pack is not supported and Machine Learning is not available for [windows</h3><p><strong>解决方法：</strong></p>
<p>在config\elasticsearch.yml最后面，添加如下配置：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">xpack.ml.enabled:</span> <span class="literal">false</span></span><br></pre></td></tr></table></figure>

<h3 id="错误3：OpenJDK-64-Bit-Server-VM-warning-INFO-os-commit-memory-0x00000000c0000000-1073741824-0-failed-error-x3D-’Not-enough-space’-errno-x3D-12"><a href="#错误3：OpenJDK-64-Bit-Server-VM-warning-INFO-os-commit-memory-0x00000000c0000000-1073741824-0-failed-error-x3D-’Not-enough-space’-errno-x3D-12" class="headerlink" title="错误3：OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c0000000, 1073741824, 0) failed; error&#x3D;’Not enough space’ (errno&#x3D;12)"></a>错误3：OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c0000000, 1073741824, 0) failed; error&#x3D;’Not enough space’ (errno&#x3D;12)</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">free -ml   查看内存使用情况</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://siyit.gitee.io/2022/07/23/2.%20Elasticsearch%20%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/" data-id="cl6bukbt4000hsu6fgz658m72" class="article-share-link">
        分享
      </a>
      
<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Elasticsearch/" rel="tag">Elasticsearch</a></li></ul>

    </footer>

  </div>

  
  
<nav class="article-nav">
  
  <a href="/2022/07/23/3.%20Elasticsearch%20%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/" class="article-nav-link">
    <strong class="article-nav-caption">前一篇</strong>
    <div class="article-nav-title">
      
      3.Elasticsearch 基本操作
      
    </div>
  </a>
  
  
  <a href="/2022/07/23/1.%20ElaticSearch%20%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/" class="article-nav-link">
    <strong class="article-nav-caption">后一篇</strong>
    <div class="article-nav-title">1.Elasticsearch 环境搭建</div>
  </a>
  
</nav>

  

  
  
<div class="vcomments" id="vcomments"></div>

<script src="https://unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  new Valine({
    el: '#vcomments',
    appId: 'A7Ny5JW4l2XoShLWoQfpND2b-gzGzoHsz',
    appKey: 'lTbAjSoXEDQETkbAcE4zpYpu',
    notify: 'true',
    verify: 'true',
    avatar: 'identicon',
    pageSize: '10',
    placeholder: '请输入...'
  })
</script>

  
  

</article>
</section>
    <footer class="footer">
  <div class="outer">
    <div class="float-right">
      <ul class="list-inline">
  
  <li><i class="fe fe-smile-alt"></i> <span id="busuanzi_value_site_uv"></span></li>
  
  <li><i class="fe fe-bookmark"></i> <span id="busuanzi_value_page_pv"></span></li>
  
</ul>
    </div>
    <ul class="list-inline">
      <li>思远程序 &copy; 2022</li>
      
        <li></li>
      
      <li>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></li>
      <li>theme  <a target="_blank" rel="noopener" href="https://github.com/zhwangart/hexo-theme-ocean">Ocean</a></li>
    </ul>
  </div>
</footer>
  </main>
  <aside class="sidebar">
    <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/hexo.svg" alt="思远程序"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/recommend">推荐</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/gallery">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/favorites">收藏</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="搜索">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      <div class="totop" id="totop">
  <i class="fe fe-rocket"></i>
</div>
    </li>
    <li class="nav-item">
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="fe fe-feed"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
  </aside>
  
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script src="/fancybox/jquery.fancybox.min.js"></script>





<script src="/js/tocbot.min.js"></script>


<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
  });
</script>



<script src="/js/ocean.js"></script>

</body>

</html>